package com.atguigu.gulimall.member.interceptor;

import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.vo.MemberResVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author 小坏
 * @Date 2021/2/2 19:58
 * @Version 1.0
 * @program: 父工程 gulimall 万物起源之地
 * <p>
 * 拦截器·
 * 想起作用还得加一个配置 OrderWebConfiguration
 */

@Component
public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberResVo> loinUser = new ThreadLocal<> ();


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //放行这个/order/order/status/** 请求路径、匹配到就放行
        //member/memberreceiveaddress/info/{id}
        //重要给WareSkuServiceImpl 这个实现类里面的远程调用使用
        String uri = request.getRequestURI ();
        boolean match = new AntPathMatcher ().match ("/member/**", uri);
        if (match) {
            return true;
        }


        MemberResVo attribute = (MemberResVo) request.getSession ().getAttribute (AuthServerConstant.LOGIN_USER);
        if (attribute != null) {
            loinUser.set (attribute);
            return true;
        } else {
            //没有就去登录
            request.getSession ().setAttribute ("msg", "请先登录");
            response.sendRedirect ("http://auth.gulimall.com/login.html");
            return false;
        }
    }
}
